package com.jacksay.vikingfl.menubar
{
	import flash.events.MouseEvent;
	
	import mx.collections.XMLListCollection;
	import mx.controls.MenuBar;
	import mx.events.MenuEvent;
	
	[Event(name="clickAction", type="com.jacksay.vikingfl.menubar.VkMenuBarEvent")]
	
	/**
	 * MenuBar supportant les click sur les items qui ne contiennent pas de sous
	 * items. Dans la description du XML, l'attribut action doit être renseigné
	 * pour les menus qui provoque une action.
	 * Les click dispatch un VkMenuBarEvent("performAction") contenant l'action.
	 */
	public class VkMenuBar extends MenuBar
	{
		public function VkMenuBar()
		{
			super();
			addEventListener(MouseEvent.CLICK, handlerMouseEventClick, false, 0, true);
			addEventListener(MenuEvent.ITEM_CLICK, handlerMenuEventItemClick, false, 0, true);
			labelField = "@label";
			iconField = "@icon";
			
		}
		
		protected function handlerMouseEventClick( e:MouseEvent ) :void {
			if( selectedIndex > -1 ){
				var menuClicked:XML = XMLListCollection(dataProvider).getItemAt(selectedIndex) as XML;
				if( menuClicked.@data != undefined && menuClicked.@data != "top" ){
					dispatchEvent(new VkMenuBarEvent(VkMenuBarEvent.CLICK_ACTION, menuClicked.@data.toString()));
					selectedIndex = -1;
				}
			}
		}
		
		protected function handlerMenuEventItemClick( e:MenuEvent ) :void {
			dispatchEvent(new VkMenuBarEvent(VkMenuBarEvent.CLICK_ACTION, e.item.@data.toString()));
		}
	}
}